//
//  TodayHomeSpo2CellViewModel.swift
//  MoyoungHome
//
//  Created by 易先生 on 2022/7/27.
//

import Foundation
import RxCocoa
import RxSwift
import MYModule

open class HomeBloodOxygenViewModel: HomeBaseViewModel {
    
    public var date: Date

    public let UIReplay: BehaviorRelay<HomeBloodOxygenModel>

    public required init(date: Date) {
        
        self.date = date
        
        let model = HomeBloodOxygenModel()
        
        UIReplay = BehaviorRelay<HomeBloodOxygenModel>(value: model)
        
        super.init(cellType: .bloodOxygen)
        
        reloadData(date)
        
        
	}
    
    // 查询单次测量血氧
    public func aBloodOxygen() -> SingleBloodOxygenModel? {
        
        guard let bloodOxygenModel = SingleBloodOxygenModel.last(from: Date().timeIntervalSince1970) else {
            return nil
        }
        
        return bloodOxygenModel
        
    }
    
    public override func reloadData(_ date: Date? = nil) {
        
        let model = reloadMeasureState()
        
        guard let singleBloodOxygenModel = aBloodOxygen() else {
            UIReplay.accept(model)
            return
        }
       
        let date = Date(timeIntervalSince1970: singleBloodOxygenModel.end)
        self.date = date
        
        model.date = Self.dateStr(date: date)
        model.bloodOxygen = singleBloodOxygenModel.o2
        
        UIReplay.accept(model)
    }
    
    
    @discardableResult
    public func reloadMeasureState() ->HomeBloodOxygenModel {
       
        
        let model = UIReplay.value
        if BluetoothManager.shared.deviceState == .connected {
            
            if BluetoothManager.shared.measuring == .bloodOxyen {
                model.measureState = .selected
            } else {
                model.measureState = (BluetoothManager.shared.measuring != .none || BluetoothManager.shared.isBatterycharging) ? .disabled : .normal
            }
            
        } else {
            model.measureState = .disabled
        }
        return model
    }
    
    
    public func automaticMeasure() {
        let model = UIReplay.value
        
        if model.measureState == .disabled {
            return
        }
        if model.measureState == .selected {
            measured()
        } else {
            measure()
        }
        
    }
    
    // 开始测量
    public func measure() {
        
        let model = UIReplay.value
       
        model.measureState = .selected
        
        BluetoothManager.shared.setStartSpO2()
        

        print("已经在测量了")
        
    }
    
    // 结束测量
    public func measured() {
        
        if BluetoothManager.shared.measuring == .bloodOxyen {
            BluetoothManager.shared.setStopSpO2()
        }
        measuredData()
        
        print("已经结束测量了")
        
    }
    
    public func measuredData() {
        
        let model = UIReplay.value
        model.measureState = .normal
        reloadData(self.date)
        
        
    }


}
